package com.smartum.mpr.client.event;

import com.google.gwt.core.client.GWT;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;
import com.smartum.mpr.client.utils.HistoryHelper;

/**
 */
public class TabClickHandler implements ClickHandler {
    private final TabSet topTabSet;
    private static int bufferedSelectionIndex;

    public TabClickHandler(TabSet topTabSet) {
        this.topTabSet = topTabSet;
    }

    @Override
    public void onClick(ClickEvent event) {
        int selectedTabNum = this.topTabSet.getSelectedTabNumber();

        if (bufferedSelectionIndex == selectedTabNum) {
            return;
        }

        switch (selectedTabNum) {
            case 0:
                GWT.log("[ NAVIGATION ] image viewer");
                bufferedSelectionIndex = selectedTabNum;
                HistoryHelper.navigateToken(HistoryHelper.ADMIN_TOKEN);
                break;
            case 1:
                GWT.log("[ NAVIGATION ] news");
                bufferedSelectionIndex = selectedTabNum;
                HistoryHelper.navigateToken(HistoryHelper.NEWS_TOKEN);
                break;
            case 2:
                GWT.log("[ NAVIGATION ] items");
                bufferedSelectionIndex = selectedTabNum;
                HistoryHelper.navigateToken(HistoryHelper.ITEMS_TOKEN);
                break;
            case 3:
                GWT.log("[ NAVIGATION ] jobs");
                bufferedSelectionIndex = selectedTabNum;
                HistoryHelper.navigateToken(HistoryHelper.JOBS_TOKEN);
                break;
        }
    }
}
